<template>
  <div style="padding: 10px">
    <!-- 按钮区域 -->

    <div style="margin: 10px 0">
      <el-button type="primary" @click="ToaddMovie">新增</el-button>
    </div>
    <h3>电影库管理</h3>
    <!-- 搜索框区域 -->

    <div style="margin: 10px 0">
      <el-input
        v-model="searchMovie"
        placeholder="请输入电影名"
        clearable
        style="width: 20%"
      />
      <el-button type="primary" style="margin-left: 10px" @click="findAll"
        >查询</el-button
      >
    </div>

    <!-- 表单区域开始 -->
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        sortable
        fixed
        width="60"
      ></el-table-column>
      <el-table-column prop="name" label="电影名" width="100"></el-table-column>
      <el-table-column prop="actor" label="主演" width="70"></el-table-column>
      <el-table-column
        prop="year"
        label="上映年份"
        width="80"
      ></el-table-column>

      <el-table-column prop="area" label="地区" sortable width="70">
        <template #default="scope">
          <span>{{
            scope.row.area == 0 ? "内地" : scope.row.area == 1 ? "港台" : "欧美"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="memo"
        style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap"
        label="描述"
        width="100"
      ></el-table-column>
      <el-table-column prop="url" label="地址" width="300"></el-table-column>
      <el-table-column
        prop="level"
        sortable
        label="评分"
        width="60"
      ></el-table-column>
      <el-table-column label="封面" width="100">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.img"
            :preview-src-list="[scope.row.img]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="gmtCreate"
        label="上传时间"
        width="100"
      ></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <!-- 加上@confirm 只有在确认被点击时才执行 -->
          <el-popconfirm
            title="确认删除吗?"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>

          <el-button type="success" size="small" @click="handleType(scope.row)"
            >添加分类</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表单区域结束 -->

    <!-- 分页开始 -->
    <div style="margin: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model:currentPage="pageNum"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 分页结束 -->

    <!-- 添加电影的弹出框 -->
    <el-dialog v-model="dialogVisible" title="添加电影" width="50%">
      <span>信息</span>
      <!-- 添加电影表单界面 -->
      <el-form :model="form" label-width="120px">
        <el-form-item label="电影名称">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="电影名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="主演">
          <el-input
            v-model="form.actor"
            style="width: 80%"
            placeholder="这个为主演"
          ></el-input>
        </el-form-item>
        <el-form-item label="年份">
          <el-input
            v-model="form.year"
            style="width: 80%"
            placeholder="年份"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="form.memo"
            style="width: 80%"
            placeholder="请输入简单描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input
            v-model="form.url"
            style="width: 80%"
            clearable
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="评分">
          <el-input
            v-model="form.level"
            style="width: 80%"
            placeholder="请输入评分"
          ></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <el-upload
            ref="addUpload"
            action="http://localhost:9090/files/upload"
            :on-success="filesUploadSuccess"
          >
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="地区">
          <el-radio-group v-model="form.area">
            <el-radio :label="0">内地</el-radio>
            <el-radio :label="1">港台</el-radio>
            <el-radio :label="2">欧美</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addMovie">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 添加电影的弹出框 结束 -->

    <!-- 修改电影的侧边框 开始 -->
    <el-drawer title="修改电影信息" v-model="drawer" :with-header="false">
      <el-form :model="form" label-width="140px">
        <el-form-item label="电影名称">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="电影名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="主演">
          <el-input
            v-model="form.actor"
            style="width: 80%"
            placeholder="这个为主演"
          ></el-input>
        </el-form-item>
        <el-form-item label="年份">
          <el-input
            v-model="form.year"
            style="width: 80%"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="form.memo"
            style="width: 80%"
            placeholder="请输入简单描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input
            v-model="form.url"
            style="width: 80%"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="评分">
          <el-input
            v-model="form.level"
            style="width: 80%"
            placeholder="请输入评分"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            ref="updateUpload"
            action="http://localhost:9090/files/upload"
            :on-success="filesUploadSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="地区">
          <el-radio-group v-model="form.area">
            <el-radio :label="0">内地</el-radio>
            <el-radio :label="1">港台</el-radio>
            <el-radio :label="2">欧美</el-radio>
          </el-radio-group>
        </el-form-item>
        <span
          class="dialog-footer"
          style="display: flex; margin: auto 0; justify-content: center"
        >
          <el-button @click="drawer = false">取 消</el-button>
          <!-- <el-button type="primary" @click="updateUser">确 定</el-button> -->
          <el-popconfirm title="确认更新吗?" @confirm="updateMovie()">
            <template #reference>
              <el-button size="mini" type="success" plain>确定</el-button>
            </template>
          </el-popconfirm>
        </span>
      </el-form>
    </el-drawer>
    <!-- 编辑界面 结束 -->

    <!-- 添加分类的弹出框 -->
    <el-dialog v-model="dialogVisibleType" title="添加电影到分类" width="30%">
      <span>不可编辑</span>
      <!-- 添加分类表单界面 -->
      <el-form :model="form" label-width="120px">
        <el-form-item label="电影名称">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="电影名称"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item label="主演">
          <el-input
            v-model="form.actor"
            style="width: 80%"
            placeholder="这个为主演"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item label="年份">
          <el-input
            v-model="form.year"
            style="width: 80%"
            placeholder="年份"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="form.memo"
            style="width: 80%"
            placeholder="请输入简单描述"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item label="评分">
          <el-input
            v-model="form.level"
            style="width: 80%"
            placeholder="请输入评分"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-radio-group v-model="form.typeId">
            <el-radio :label="1">大片</el-radio>
            <el-radio :label="2">独家</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisibleType = false">取 消</el-button>
          <el-button type="primary" @click="addType">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 添加分类的弹出框 结束 -->
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: "Movie",
  components: {},
  data() {
    return {
      searchMovie: "", //定义输入框
      currentPage: 1, //默认是1r
      pageSize: 10, //默认为10
      pageNum: 1,
      total: 0,
      dialogVisible: false, //默认关闭 (添加电影弹出框)
      dialogVisibleType: false,
      form: {}, //定义form对象为空
      tableData: [],
      sex: 0,
      drawer: false, //默认不显示抽屉
      type: 0,
      
    };
  },
  //页面加载时
  created() {
    this.findAll();
  },

  //方法写在data外面
  methods: {
    //添加到分类,大片或者独家
    handleType(row) {
      //深拷贝,避免取消了被改变
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisibleType = true;
      //处理未来元素执行发生不存在时的错误
      this.$nextTick(() => {
        this.$refs["updateUpload"].clearFiles();
      });
    },
    //添加到分类,大片或独家
    addType() {
      request.put("/type/addType/", this.form).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "添加到分类成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
        //执行完了刷新界面
        //this.findAll();
        //关闭弹出框
        this.dialogVisibleType = false;
      });
    },

    //上传封面
    filesUploadSuccess(res) {
      console.log("上传的封面图片: " + res);
      this.form.img = res.data;
    },

    //查询所有电影
    findAll() {
      console.log(this.searchMovie);
      request
        .get("/movie/findAll", {
          params: {
            //传入变量
            pageSize: this.pageSize,
            pageNum: this.pageNum,
            name: this.searchMovie,
          },
        })
        .then((res) => {
          console.log(res);
          //重点 将查询到的数据records 返回给表单中的tableData
          (this.tableData = res.data.list), (this.total = res.data.total);
        });
    },

    //弹出添加电影框
    ToaddMovie() {
      //显示添加页面
      this.dialogVisible = true;
      //清除历史文件列表
      this.$refs["addUpload"].clearFiles();
      //清空表单存在过的值
      this.form = {};
      // this.$me;
    },
    //添加电影操作
    addMovie() {
      request.post("/movie/addMovie/", this.form).then((res) => {
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "添加成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
        //执行完了刷新界面
        this.findAll();
        //关闭弹出框
        this.dialogVisible = false;
      });
    },

    //编辑拷贝
    handleEdit(row) {
      //深拷贝,避免取消了被改变
      this.form = JSON.parse(JSON.stringify(row));
      this.drawer = true;
      //处理未来元素执行发生不存在时的错误
      this.$nextTick(() => {
        this.$refs["updateUpload"].clearFiles();
      });
    },
    //更新电影
    updateMovie() {
      request.put("/movie/updateMovie/", this.form).then((res) => {
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "更新成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
        //执行完了刷新界面
        this.findAll();
        //关闭弹出框
        this.drawer = false;
      });
    },

    //删除电影
    handleDelete(id) {
      console.log(id);

      request.put("/movie/deleteMovie/" + id).then((res) => {
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "删除成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
        this.findAll(); //重新加载删除之后表格的数据
      });
    },

    //改变条数
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.findAll();
    },

    //改变当前页码
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.findAll();
    },
  },
};
</script>
